package com.oner365.test.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * java8 stream 并行流小demo
 */
public class ParallelStreamDemo {

  private static final Logger LOGGER = LoggerFactory.getLogger(ParallelStreamDemo.class);

  public static void main(String[] args) throws InterruptedException {
    
    routineMethod();
    //并行
    LOGGER.info("=================================");
    parallelMethod();
  }
  

  // 一般逻辑写法
  public static void routineMethod() throws InterruptedException {
    LOGGER.info("一般逻辑数据收集");
    long start = System.currentTimeMillis();
    Result result = new Result();
    result.setTotalUser(queryTotalUser());
    result.setActiveUser(queryActiveUser());
    result.setOrderInfo(queryOrderInfo());
    result.setTotalOrder(queryTotalOrder());
    LOGGER.info("用时:{}", (System.currentTimeMillis() - start) / 1000);
  }

  // 并行逻辑写法
  public static <V> void parallelMethod() throws InterruptedException {
    LOGGER.info("并行数据收集");
    long start = System.currentTimeMillis();
    Result result = new Result();
    List<Runnable> list = new ArrayList<Runnable>() {
      private static final long serialVersionUID = 1L;
      {
        add(() -> result.setActiveUser(queryTotalUser()));
        add(() -> result.setTotalUser(queryActiveUser()));
        add(() -> result.setOrderInfo(queryOrderInfo()));
        add(() -> result.setTotalOrder(queryTotalOrder()));
      }
    };
    list.parallelStream().forEach(task -> task.run());
    LOGGER.info("用时:{}", (System.currentTimeMillis() - start) / 1000);
  }

  // 模拟大数据量复杂业务逻辑收集数据
  // queryTotalUser、queryTotalOrder、queryActiveUser、queryOrderInfo
  public static int queryTotalUser() {
    try {
      TimeUnit.SECONDS.sleep(2);
    } catch (InterruptedException e) {
      LOGGER.error("queryTotalUser error:", e);
    }
    LOGGER.info("执行:queryTotalUser");
    return 10000;
  }

  public static int queryTotalOrder() {
    try {
      TimeUnit.SECONDS.sleep(2);
    } catch (InterruptedException e) {
      LOGGER.error("queryTotalOrder error:", e);
    }
    LOGGER.info("执行:queryTotalOrder");
    return 10000;
  }

  public static int queryActiveUser() {
    try {
      TimeUnit.SECONDS.sleep(3);
    } catch (InterruptedException e) {
      LOGGER.error("queryActiveUser error:", e);
    }
    LOGGER.info("执行:queryActiveUser");
    return 10000;
  }

  public static String queryOrderInfo() {
    try {
      TimeUnit.SECONDS.sleep(3);
    } catch (InterruptedException e) {
      LOGGER.error("queryOrderInfo error:", e);
    }
    LOGGER.info("执行:queryOrderInfo");
    return "ok";
  }

  public static class Result {
    int totalUser;
    int totalOrder;
    int activeUser;
    String orderInfo;

    /**
     * @return the totalUser
     */
    public int getTotalUser() {
      return totalUser;
    }

    /**
     * @param totalUser the totalUser to set
     */
    public void setTotalUser(int totalUser) {
      this.totalUser = totalUser;
    }

    /**
     * @return the totalOrder
     */
    public int getTotalOrder() {
      return totalOrder;
    }

    /**
     * @param totalOrder the totalOrder to set
     */
    public void setTotalOrder(int totalOrder) {
      this.totalOrder = totalOrder;
    }

    /**
     * @return the activeUser
     */
    public int getActiveUser() {
      return activeUser;
    }

    /**
     * @param activeUser the activeUser to set
     */
    public void setActiveUser(int activeUser) {
      this.activeUser = activeUser;
    }

    /**
     * @return the orderInfo
     */
    public String getOrderInfo() {
      return orderInfo;
    }

    /**
     * @param orderInfo the orderInfo to set
     */
    public void setOrderInfo(String orderInfo) {
      this.orderInfo = orderInfo;
    }

  }
}
